package main

import (
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"github.com/robfig/cron"
	"src/config"
	"src/controller"
	mid "src/middleware"
	"src/model"
	"src/view"
)

var debug bool

func main() {
	debug = false
	if config.Conf.AppInfo.Env == "dev" {
		debug = true
	}
	model.Forumpostinit()
	go startTimer()
	startWeb()
}

func startWeb() {
	e := echo.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	v1 := e.Group("/api/v1")

	if debug {
		skipperPath := []string{
			"/api/v1/comment/reply/list/comment",
			"/api/v1/comment/reply/list/forumpost",
			"/api/v1/comment/list",
			"/api/v1/forumpost/list",
			"/api/v1/login",
			"/api/v1/login/test",
			"/api/v1/hotkey",
		}
		v1.Use(middleware.JWTWithConfig(mid.CustomJWTConfig(skipperPath, "Bearer")))
	}

	//admin := v1.Group("/admin")
	//view.InitAdminView(admin)

	message := v1.Group("/message")
	view.InitMessageView(message)

	comment := v1.Group("/comment")
	view.InitCommentView(comment)

	forumpost := v1.Group("/forumpost")
	view.InitForumpostView(forumpost)

	user := v1.Group("/user")
	view.InitUserView(user)

	application := v1.Group("/application")
	view.InitApplicationView(application)

	hotkey := v1.Group("/hotkey")
	view.InitHotkeyView(hotkey)

	search := v1.Group("/search")
	view.InitSearchView(search)

	v := v1.Group("")
	view.InitIndexView(v)
	// 启动
	e.Logger.Fatal(e.Start(config.Conf.AppInfo.Addr))
	println("启动")

}

func startTimer() {
	c := cron.New()
	_ = c.AddFunc(config.DefaultCronTime, controller.StartTimerTask)
	_ = c.AddFunc(config.WeixinAccess_tokenTime, model.RefreshWeixinAccessToken)
	c.Start()
}
